package com.zuo.demo01.controller.blogs;


import com.zuo.demo01.pojo.Blog;
import com.zuo.demo01.pojo.Tag;
import com.zuo.demo01.service.BlogService;
import com.zuo.demo01.service.ConfigService;
import com.zuo.demo01.service.LinkService;
import com.zuo.demo01.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BlogsController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private ConfigService configService;

    @Autowired
    private TagService tagService;

    @Autowired
    private LinkService linkService;

    @RequestMapping({"/","/index"})
    public String index(Model model){
        return this.page(1,10,model);
    }

    @RequestMapping("/blog/page")
    public String page(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, Model model){
        Map<String,Object> map =new HashMap<>();
        map.put("startIndex",(page-1)*limit);
        map.put("pageSize",limit);
        List<Blog> blogs = blogService.findAllPub(map);
        model.addAttribute("blogs",blogs);

        Map<String, String> configs = configService.findAll();
        model.addAttribute("configs",configs);

        List<Tag> tags = tagService.findAll();
        model.addAttribute("tags",tags);


        List<Blog> newBlogs = blogService.findByType(0, 10);
        model.addAttribute("newBlogs",newBlogs);

        List<Blog> hotBlogs = blogService.findByType(1, 10);
        model.addAttribute("hotBlogs",hotBlogs);


        model.addAttribute("count",blogService.countByPub());
        model.addAttribute("curr",page);
        return "blogs/index";
    }




    @RequestMapping("/links")
    public String links(Model model){
        model.addAttribute("links",linkService.getAll());
        Map<String, String> configs = configService.findAll();
        model.addAttribute("configs",configs);
        return "blogs/links";
    }


    @RequestMapping("/about")
    public String about(){
        return "blogs/about";
    }

    @RequestMapping("/blog/{id}")
    public String blog(@PathVariable("id") Integer id){
        return "blogs/blog";
    }




}
